<?php

if (!function_exists('sign')) {
  /**
   * 签名计算
   *
   * @param array $args
   * @param string $secret
   * @return string
   */
  function sign(array $args, $secret)
  {
    $str = '';
    ksort($args);
    foreach ($args as $key => $value) {
      $str .= $key . '=' . $value . '&';
    }
    $sign_str = rtrim($str, '&');
    $sign = md5($sign_str . $secret);
    return $sign;
  }
}

if (!function_exists('randomStr')) {
  /**
   * 生成随机码
   *
   * @param int $length
   * @param string $mode
   * @return string
   */
  function randomStr($length, $mode = 'mix')
  {
    $hash = '';
    $chars_temp = [
      'upper'     => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
      'lower'     => 'abcdefghijklmnopqrstuvwxyz',
      'upper_int' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
      'lower_int' => 'abcdefghijklmnopqrstuvwxyz0123456789',
      'int'       => '0123456789',
      'mix'       => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'

    ];

    $chars = key_exists($mode, $chars_temp) ? $chars_temp[$mode] : "";
    if (empty($chars)) {
      return false;
    } else {
      $max = strlen($chars) - 1;
      PHP_VERSION < '4.2.0' && mt_srand((float)microtime() * 1000000);
      for ($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
      }
      return $hash;
    }
  }
}
